#!/bin/bash

# remove development packages
apt-get -y --purge remove build-essential sparse $(dpkg --get-selections | grep -v deinstall$ | cut -f1 | grep -P -- '-dev(:\w+)?$' | grep -Pv -- '^(dpkg|libgcc)') || true

# remove unwanted packages
apt-get -y --purge remove bluez-firmware \
                          docker-buildx-plugin \
                          docker-ce-rootless-extras \
                          gdebi* \
                          firmware-ast \
                          firmware-b43-installer \
                          firmware-b43legacy-installer \
                          firmware-carl9170 \
                          firmware-cavium \
                          firmware-cirrus \
                          firmware-intel-sound \
                          firmware-ipw2x00 \
                          firmware-ivtv \
                          firmware-libertas \
                          firmware-marvell-prestera \
                          firmware-myricom \
                          firmware-netronome \
                          firmware-netxen \
                          firmware-nvidia* \
                          firmware-qcom-soc \
                          firmware-qlogic \
                          firmware-siano \
                          firmware-sof-signed \
                          firmware-zd1211 \
                          gdb \
                          gnome-accessibility-themes \
                          libasound2-plugins \
                          libx264* \
                          nvidia* \
                          pavucontrol \
                          poppler-data \
                          pulseaudio \
                          tango-icon-theme \
                          xdg-user-dirs \
                          yelp || true

apt-get -y --purge autoremove
apt-get clean

# hold packages we don't want to update with an apt-get upgrade
# we built htpdate from source for HTTPS support, so leave it
dpkg -s htpdate >/dev/null 2>&1 && apt-mark hold htpdate

# remove any residual configs
dpkg -l | awk '/^rc/ { print $2 }' | xargs -r -l dpkg --purge

# disable automatic/initial running of some services (but don't abort if we fail)
systemctl disable ctrl-alt-del.target || true
systemctl disable apt-daily.service || true
systemctl disable apt-daily.timer || true
systemctl disable apt-daily-upgrade.timer || true
systemctl disable apt-daily-upgrade.service || true
